package linkedlist;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/3/26
 * Time: 18:31
 */
public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        HeroNode2 hero1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2 = new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4 = new HeroNode2(4,"公孙胜","入云龙");
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero4);
        HeroNode2 test = doubleLinkedList.get(3);
//        doubleLinkedList.del(3);
        doubleLinkedList.list();
    }




}

class DoubleLinkedList {
    //先初始化一个头节点，头结点不要动，不存放具体的数据
    private HeroNode2 herd = new HeroNode2(0, "", "");

    public HeroNode2 getHerd() {
        return herd;
    }

    public void list() {
        //判断链表是否为空
        if (herd.next == null) {
            System.err.println("链表为空");
            return;
        }
        //因为头节点不能动，需要一个辅助变量
        HeroNode2 temp = herd.next;
        while (true) {
            //输出节点的信息
            System.out.println(temp);
            //判断是否链表到最后
            if (temp.next == null) {
                break;
            }
            //将temp后移
            temp = temp.next;
        }
    }

    //添加节点到单向链表
    public void add(HeroNode2 herdNode) {
        HeroNode2 temp = herd;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = herdNode;
        herdNode.pre = temp;
    }

    public void update(HeroNode2 newHeroNode) {
        //判断是否空
        if (herd.next == null) {
            System.err.println("链表为空");
            return;
        }
        HeroNode2 temp = herd.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == newHeroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag判断是否找到要修改的节点
        if (flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        } else {
            System.out.printf("没有找到编号%d的节点。不能修改\n", newHeroNode.no);
        }
    }

    public void del(int no) {
        HeroNode2 temp = herd.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if(temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.pre.next = temp.next;
            if(temp.next != null) { // 说明不是删除最后一个
                temp.next.pre = temp.pre;
            }
        }else {
            System.err.printf("要删除的编号为%d节点不存在",no);
        }
    }

    public HeroNode2 get(int no) {
        HeroNode2 temp = herd.next;
        while (temp != null) {
            if(temp.no == no) {
                break;
            }
            temp = temp.next;
        }
        return temp;
    }

    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = herd;
        boolean flag = false; // flag标记添加的编号是否存在
        while(true) {
            if(temp.next == null) {
                break;
            }
            if(temp.next.no > heroNode.no) {
                break;
            }else if(temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            System.out.println("编号" + heroNode.no + "的英雄已存在，无法加入");
        }else {
            //插入到链表中，temp的后面
            heroNode.next = temp.next;
            if(temp.next != null) {
                temp.next.pre = heroNode;
            }
            temp.next = heroNode;
            heroNode.pre = temp;
        }
    }
}
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;

    public HeroNode2 next; //指向下一个节点
    public HeroNode2 pre;  //指向上一个节点
    //构造器
    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //重新toString

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname  + '\''+
                '}';
    }
}